package com.common.util;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author: stefanie-zy
 * @time: 2024/1/29-9:38
 * @description: String工具类集成
 */
@Component
@Slf4j
@Schema(name = "工具-字符串/对象")
public class StringUtil {

    private static final String STR_NULL = "";

    /**
     * 判断对象是否为空
     *
     * @param object 对象
     */
    public static boolean isNull(Object object) {
        return null == object;
    }

    /**
     * 判断对象不为NULL
     *
     * @param object 对象
     */
    public static boolean isNotNull(Object object) {
        return !isNull(object);
    }

    /**
     * 判断字符串是否为空
     *
     * @param str 字符串
     */
    public static boolean isEmpty(String str) {
        return isNull(str) || STR_NULL.equals(str.trim());
    }

    /**
     * 判断字符串不为空
     *
     * @param str 字符串
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 判断数组是否为空
     *
     * @param objects 数组
     */
    public static boolean isEmpty(Object[] objects) {
        return isNull(objects) || (objects.length == 0);
    }

    /**
     * 判断数组不为空
     *
     * @param objects 数组
     */
    public static boolean isNotEmpty(Object[] objects) {
        return !isNull(objects);
    }

    /**
     * 判断Map是否为空
     *
     * @param map Map
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return isNull(map) || map.isEmpty();
    }

    /**
     * 判断Map不为空
     *
     * @param map Map
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    /**
     * 去字符串空格
     * @param str 字符串
     */
    public static String trim(String str) {
        return (str == null ? "" : str.trim());
    }

    /**
     * 截取字符串
     * @param str 字符串
     * @param start 起始位
     */
    public static String subString(String str, int start) {
        if (isEmpty(str)) {
            return STR_NULL;
        }
        if (!(0 <= start)) {
            start = str.length() + start;
        }
        if (!(0 <= start)) {
            start = 0;
        }
        if (start > str.length()) {
            return STR_NULL;
        }
        return str.substring(start);
    }

    /**
     * 截取字符串
     * @param str 字符串
     * @param start 起始位
     * @param end 结束位
     */
    public static String subString(String str, int start, int end) {
        if (isEmpty(str)) {
            return STR_NULL;
        }
        if (!(0 <= end)) {
            end = str.length() + end;
        }
        if (!(0 <= start)) {
            start = str.length() + start;
        }
        if (end > str.length()) {
            end = str.length();
        }
        if (start > end) {
            return STR_NULL;
        }
        if (!(0 <= start)) {
            start = 0;
        }
        if (!(0 <= end)) {
            end = 0;
        }
        return str.substring(start, end);
    }
}
